package com.zjr.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.util.tomcat.buf.UDecoder;
import org.springframework.transaction.annotation.Transactional;

import com.zjr.dao.UserDao;
import com.zjr.domain.User;
import com.zjr.service.UserService;
import com.zjr.utils.Page;
import com.zjr.utils.UserUtil;
import com.zjr.utils.ViewUser;
/**
 * 用户业务层实现类
 * */
@Transactional
public class UserServiceImpl implements UserService{
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	//用户登入
	@Override
	public User login(User user) {
		return userDao.login(user);
	}
	
	//用户添加
	@Override
	public boolean save(User user) {
		return userDao.save(user);
	}
	
	//查询所有用户的信息, 将用户转化以便页面显示
	@Override
	public List<ViewUser> findAll() {
		List<User>list =  userDao.findAll();
		List<ViewUser> list_vu = new ArrayList<ViewUser>();
		for(User user : list){
			list_vu.add(UserUtil.change(user));
		}
		return list_vu;
	}
	
	@Override
	public User findById(int uid) {
		return userDao.findById(uid);
	}

	@Override
	public void modify(User user) {
		userDao.modify(user);
	}

	@Override
	public void delete(int uid) {
		userDao.delete(uid);
	}

	@Override
	public List<User> findByDid(int did) {
		return userDao.findByDid(did);
	}
	
	//分页查询用户
	@Override
	public List<ViewUser> queryUser(Page page) {
		int totalNumber = userDao.count();
		page.setTotalNumber(totalNumber);
		List<User> list = userDao.queryUser(page);
		List<ViewUser> list_vu = new ArrayList<ViewUser>();
		for(User user : list){
			list_vu.add(UserUtil.change(user));
		}
		return list_vu;
	}

}
